from django.contrib.auth.models import User
from django.db import models


# Create your models here.

class BaseModel(models.Model):
    created_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True, help_text='创建时间')
    last_modified = models.DateTimeField(verbose_name='修改时间', auto_now=True, help_text='修改时间')

    class Meta:
        abstract = True


class Topic(BaseModel):
    title = models.CharField(verbose_name='标题', max_length=255, unique=True, help_text='话题标题')
    content = models.TextField(verbose_name='内容', help_text='话题内容')
    is_online = models.BooleanField(verbose_name='是否在线', default=True, help_text='话题是否在线')
    # 用户 多对一关系
    user = models.ForeignKey(verbose_name='发布者', to=User, on_delete=models.CASCADE, null=True)

    class Meta:
        ordering = ['-last_modified']
        verbose_name = '话题'
        verbose_name_plural = '话题列表'

class Comment(BaseModel):
    content = models.CharField(verbose_name='评论', max_length=255, help_text='话题评论')
    up = models.IntegerField(verbose_name='支持数', default=0, help_text='支持数')
    down = models.IntegerField(verbose_name='反对数', default=0, help_text='反对数')
    # 话题 多对一关系
    topic = models.ForeignKey(to=Topic, on_delete=models.SET_NULL, help_text='关联话题表', null=True)
    # 用户 多对一关系
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)

    class Meta:
        verbose_name = '评论'
        verbose_name_plural = '评论列表'


class Role(models.Model):
    name = models.CharField("角色名称", max_length=50, unique=True)
    # 多对多
    users = models.ManyToManyField(to=User)
